package concurrent;

import sun.util.calendar.LocalGregorianCalendar;

import javax.xml.crypto.Data;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author Summerday
 */
public class TestThreadPoolExecutor {

    private static final int CORE_POOL_SIZE = 5; //核心线程数
    private static final int MAX_POOL_SIZE = 10; //最大线程数
    private static final int QUEUE_CAPACITY = 5; //任务队列的容量
    private static final long KEEP_ALIVE_TIME = 1L; //等待时间
    static ThreadPoolExecutor threadPool = new ThreadPoolExecutor(
            CORE_POOL_SIZE,
            MAX_POOL_SIZE,
            KEEP_ALIVE_TIME,
            TimeUnit.SECONDS,
            new ArrayBlockingQueue<>(QUEUE_CAPACITY),
            new ThreadPoolExecutor.AbortPolicy());

    public static void main (String[] args) {

        for(int i = 1; i <= 10; i ++){
            Thread thread = new Thread(() -> {
                String name = Thread.currentThread().getName();
                System.out.println(name + " start :" + new Date());
                try {
                    TimeUnit.SECONDS.sleep(5);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(name + " end   :" + new Date());
            }, "thread - " + i);
            threadPool.execute(thread);
        }
        threadPool.shutdown();
        System.out.println(threadPool.isShutdown());
        threadPool.execute(new Thread(()->{
            System.out.println("execute!");
        }));
        System.out.println("finish !");
    }


}
